package com.exam.controller;

import com.exam.dataobject.User;
import com.exam.enums.UserStatusEnum;
import com.exam.exception.ExaminationException;
import com.exam.repository.UserRepository;
import com.exam.service.UserService;
import com.exam.utils.RedisUserIdUtil;
import com.exam.utils.ResultVOUtil;
import com.exam.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created by v
 * Date:2019/1/6
 * Time:20:24
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {


    @Autowired
    private UserService userService;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 验证用户是否已被注册
     * @param userName
     * @return
     */
    @GetMapping("/username/exist")
    public ResultVO userNameExist(@RequestParam(value = "userName") String userName) {

        User user = userRepository.findByUserName(userName);
        if (user != null) {
            log.error("【用户名已经被注册】 userName={}", userName);
            throw new ExaminationException(UserStatusEnum.USER_NAME_EXIST.getCode(), UserStatusEnum.USER_NAME_EXIST.getMessage());
        }
        userService.ifUserExist(userName);
        return ResultVOUtil.success();
    }

    /**
     * 用户注册
     * @param registerInfo
     * @param bindingResult
     * @param request
     * @return
     */
    @PostMapping("/register")
    public ResultVO insertRegister(@RequestBody Map<String, Object> registerInfo, BindingResult bindingResult,
                                   HttpServletRequest request) {

        userService.saveRegisterUser(registerInfo, bindingResult, request);
        return ResultVOUtil.success();
    }

    /**
     * 用户登录
     * @param userLogin
     * @param request
     * @param httpResponse
     * @return
     */
    @PostMapping("/login")
    public ResultVO login(@RequestBody Map<String, Object> userLogin, HttpServletRequest request,
                          HttpServletResponse httpResponse) {

        Map<String, Object> map = userService.login(userLogin, request, httpResponse);
        return ResultVOUtil.success(map);
    }

    /**
     * 前段传递get请求，根据cookie里面的token判断是否已经登录,若登录则返回用户id和类型
     *
     * @param request
     * @return
     */
    @GetMapping("/is/login")
    public ResultVO isLogin(HttpServletRequest request) {

        return userService.isLogin(request);
    }

    /**
     * 登出
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/logout")
    public ResultVO logout(HttpServletRequest request,
                           HttpServletResponse response) {
        userService.logout(request, response);
        return ResultVOUtil.success("登出成功");
    }

    /**
     * 用户基本信息查看
     * @param request
     * @return
     */
    @GetMapping("/info")
    public ResultVO userInfo(HttpServletRequest request) {

        String userId = RedisUserIdUtil.getUserId(request, redisTemplate);

        Map<String, Object> map = userService.getUserInfo(Integer.valueOf(userId));
        return ResultVOUtil.success(map);
    }


    /**
     * 信息修改
     * @param infoChange
     * @param request
     * @return
     */
    @PostMapping("/info/change")
    public ResultVO userInfoChange(@RequestBody Map<String, Object> infoChange, HttpServletRequest request) {

        userService.changeUserInfo(infoChange, request);
        return ResultVOUtil.success();
    }
}
